
package com.xz.stu.server.job;

import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMapper;
import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.xz.stu.server.entity.CalendarEvent;
import com.xz.stu.server.service.CalendarEventService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Component
@EnableScheduling
@Slf4j
public class NoticeJob {

    @Resource
    private NotifyMessageSendApi notifySendApi;
    @Resource
    private AdminUserService adminUserService;
    @Resource
    private CalendarEventService calendarEventService;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Scheduled(cron = "0 0 10 * * ? ")
    public void notice() {
        log.info("[通知任务notice][开始执行]");
        List<CalendarEvent> calendarEvents = calendarEventService.list();
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        List<CalendarEvent> upcomingEvents = calendarEvents.stream().filter(calendarEvent -> {
            LocalDateTime eventTime = calendarEvent.getEventDate();
            return eventTime.isAfter(now) && eventTime.isBefore(now.plusDays(3));
                }).collect(Collectors.toList());
        //过滤出管理员消息
        List<RoleDO> roleDOS = roleMapper.selectList(RoleDO::getName, Arrays.asList("管理员", "超级管理员"));
        List<Long> userRoleDOS = userRoleMapper.selectList(UserRoleDO::getRoleId, roleDOS.stream().map(RoleDO::getId).collect(Collectors.toList()))
                .stream().map(UserRoleDO::getUserId).collect(Collectors.toList());
        List<CalendarEvent> adminNotice = upcomingEvents.stream()
                .filter(calendarEvent -> userRoleDOS.contains(Long.valueOf(calendarEvent.getCreator())))
                .collect(Collectors.toList());
        List<CalendarEvent> userNotice = upcomingEvents.stream()
                .filter(calendarEvent -> !userRoleDOS.contains(Long.valueOf(calendarEvent.getCreator())))
                .collect(Collectors.toList());
        //用户id
        List<Long> userIds = adminUserService.lambdaQuery().notIn(AdminUserDO::getId, userRoleDOS)
                .list()
                .stream().map(AdminUserDO::getId).collect(Collectors.toList());
        //先发送管理员消息
        for (CalendarEvent calendarEvent : adminNotice) {
            for (Long userId : userIds) {
                NotifySendSingleToUserReqDTO reqDTO = new NotifySendSingleToUserReqDTO();
                reqDTO.setUserId(userId);
                reqDTO.setTemplateCode("notice"); // 设置消息内容
                HashMap<String, Object> map = new HashMap<>();
                map.put("eventDate", calendarEvent.getEventDate());
                map.put("title", calendarEvent.getTitle());
                map.put("remark",calendarEvent.getRemark());
                reqDTO.setTemplateParams(map);
                notifySendApi.sendSingleMessageToAdmin(reqDTO);
            }
        }
        //发送用户消息
        for (CalendarEvent calendarEvent : userNotice) {
            NotifySendSingleToUserReqDTO reqDTO = new NotifySendSingleToUserReqDTO();
            reqDTO.setUserId(Long.valueOf(calendarEvent.getCreator()));
            reqDTO.setTemplateCode("notice"); // 设置消息内容
            HashMap<String, Object> map = new HashMap<>();
            map.put("eventDate", calendarEvent.getEventDate());
            map.put("title", calendarEvent.getTitle());
            map.put("remark",calendarEvent.getRemark());
            reqDTO.setTemplateParams(map);
        }

        log.info("[通知任务notice][执行完成]");
    }


}
